﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Fcdbas.WinUI.Forms;

using Fcdbas.Platform.Interface;
using Fcdbas.WinUI.Common;
using Fcdbas.Core.Windows;
using Fcdbas.WinUI.Windows;
using Fcdbas.Platform.Properties;
using Fcdbas.Platform;
using Fcdbas.WinUI.Controls;
using Fcdbas.Core;
using Fcdbas.Biz;

namespace Fcdbas.WinUI.Forms
{
    public partial class dlgWindowAttribute : cDialogWindow
    {
        private WindowProfile WProfile;
       // private UserQueryProfile QProfile;
        private BaseForm parentFrom;
        private bool IsLoading = true;
        public dlgWindowAttribute(BaseForm frm,WindowProfile wpfile)
        {
            InitializeComponent();
            WProfile = wpfile;
         //   QProfile = qpfile;
            parentFrom = frm;
            BindToUI();
        }
        private void BindToUI()
        {
            IsLoading = true;
            this.txtName.Text = WProfile.PreObject;
            this.txtRecordLimit.Text = WProfile.RecordLimit.ToString();
            this.rgBehavior.SelectedIndex = (int)WProfile.Behavior;
            SetReadOnly(false);
            SetRequire(false);
            IsLoading = false;
        }
        private void SetReadOnly(bool reset)
        {
            if(parentFrom.CurrentSourceBinding == null)
                return;
            string dsName = parentFrom.CurrentSourceBinding.Name ;
            DataSourceSetting dset = WProfile.DataSourceItems.Find(p => p.Name == dsName );
            if(dset == null)
            {
                dset = new DataSourceSetting ();
                dset.Name = dsName ;
                dset.ReadOnlyItems = new List<ItemSetting> ();
                dset.RequireItems = new List<ItemSetting> ();
                WProfile.DataSourceItems.Add(dset);
            }
            List<ItemSetting> ReadOnlyItems = dset.ReadOnlyItems;
            if (reset)
                ReadOnlyItems.Clear();
            if (ReadOnlyItems != null && ReadOnlyItems.Count > 0)
            {

                if (parentFrom.CurrentSourceBinding != null && parentFrom.CurrentSourceBinding.Columns != null && ReadOnlyItems.Count != parentFrom.CurrentSourceBinding.Columns.Count)
                {
                    foreach (DataItem di in parentFrom.CurrentSourceBinding.Columns)
                    {
                        if (ReadOnlyItems.Find(p => p.ColumnName == di.ColumnName) == null)
                        {
                            ItemSetting item = new ItemSetting();
                            item.Caption = di.Caption;
                            item.ColumnName = di.ColumnName;
                            if (di.UpdateOption == Platform.Enums.DataItemUpdate.NotAllowed)
                            {
                                item.IsSet = true;
                                item.IsDesignSet = true;
                            }
                            else
                            {
                                item.IsSet = false;
                                item.IsDesignSet = false;
                            }
                            ReadOnlyItems.Add(item);
                        }
                    }
                }
            }
            else
            {
                if (dset.ReadOnlyItems == null)
                {
                    dset.ReadOnlyItems = new List<ItemSetting>();
                    ReadOnlyItems = dset.ReadOnlyItems;
                }
                if (parentFrom.CurrentSourceBinding != null && parentFrom.CurrentSourceBinding.Columns != null)
                {
                    foreach (DataItem di in parentFrom.CurrentSourceBinding.Columns)
                    {

                        ItemSetting item = new ItemSetting();
                        item.Caption = di.Caption;
                        item.ColumnName = di.ColumnName;
                        if (di.UpdateOption == Platform.Enums.DataItemUpdate.NotAllowed)
                        {
                            item.IsSet = true;
                            item.IsDesignSet = true;
                        }
                        else
                        {
                            item.IsSet = false;
                            item.IsDesignSet = false;
                        }
                        ReadOnlyItems.Add(item);
                    }
                }
            }
            clbReadOnly.DisplayMember = "Caption";
            clbReadOnly.ValueMember = "IsSet";
            clbReadOnly.DataSource = ReadOnlyItems;
            int i = 0;
            while (clbReadOnly.GetItem(i) != null)
            {
                
                clbReadOnly.SetItemCheckState(i, (true.Equals(clbReadOnly.GetItemValue(i)) ? CheckState.Checked : CheckState.Unchecked));
                i++;
            }

        }
        private void SetRequire(bool reset)
        {
            if (parentFrom.CurrentSourceBinding == null)
                return;
            string dsName = parentFrom.CurrentSourceBinding.Name;
            DataSourceSetting dset = WProfile.DataSourceItems.Find(p => p.Name == dsName);
            if (dset == null)
            {
                dset = new DataSourceSetting();
                dset.Name = dsName;
                dset.ReadOnlyItems = new List<ItemSetting>();
                dset.RequireItems = new List<ItemSetting>();
                WProfile.DataSourceItems.Add(dset);
            }
            List<ItemSetting> RequireItems = dset.RequireItems;
            if (reset)
                RequireItems.Clear();
            if (RequireItems != null && RequireItems.Count > 0)
            {

                if (parentFrom.CurrentSourceBinding != null && parentFrom.CurrentSourceBinding.Columns != null && RequireItems.Count != parentFrom.CurrentSourceBinding.Columns.Count)
                {
                    foreach (DataItem di in parentFrom.CurrentSourceBinding.Columns)
                    {
                        if (RequireItems.Find(p => p.ColumnName == di.ColumnName) == null)
                        {
                            ItemSetting item = new ItemSetting();
                            item.Caption = di.Caption;
                            item.ColumnName = di.ColumnName;
                            item.IsSet = di.Required;
                            item.IsDesignSet = di.Required;
                            RequireItems.Add(item);
                        }
                    }
                }
            }
            else
            {
                if (dset.RequireItems == null)
                {
                    
                    dset.RequireItems = new List<ItemSetting>();
                    RequireItems = dset.RequireItems;
                }
                if (parentFrom.CurrentSourceBinding != null && parentFrom.CurrentSourceBinding.Columns != null)
                {
                    foreach (DataItem di in parentFrom.CurrentSourceBinding.Columns)
                    {

                        ItemSetting item = new ItemSetting();
                        item.Caption = di.Caption;
                        item.ColumnName = di.ColumnName;
                        item.IsSet = di.Required;
                        item.IsDesignSet = di.Required;
                        RequireItems.Add(item);
                    }
                }
            }
            clbRequired.DisplayMember = "Caption";
            clbRequired.ValueMember = "IsSet";
            clbRequired.DataSource = RequireItems;
            int i = 0;
            while (clbRequired.GetItem(i) != null)
            {
                
                clbRequired.SetItemCheckState(i, (true.Equals(clbRequired.GetItemValue(i)) ?  CheckState.Checked : CheckState.Unchecked ));
                i++;
            }
        }
        private void BindToEntity()
        {
            WProfile.RecordLimit = string.IsNullOrEmpty(this.txtRecordLimit.Text) ? 0 : int.Parse(this.txtRecordLimit.Text);
            WProfile.Behavior = (WSBehavior)this.rgBehavior.SelectedIndex;
            if (parentFrom.CurrentSourceBinding == null)
                return;
            string dsName = parentFrom.CurrentSourceBinding.Name;
            DataSourceSetting dset = WProfile.DataSourceItems.Find(p => p.Name == dsName);
            if (dset == null)
                return;
            int i = 0;
            while (clbRequired.GetItem(i) != null)
            {

                dset.RequireItems[i].IsSet = clbRequired.GetItemChecked(i);
                i++;
            }

            foreach (ItemSetting item in dset.RequireItems)
            {
                DataItem di = parentFrom.CurrentSourceBinding.Columns.Find(p => p.ColumnName == item.ColumnName);
                if (di != null)
                {
                    di.Required = item.IsSet;
                }
            }
            i = 0;
            while (clbReadOnly.GetItem(i) != null)
            {

                dset.ReadOnlyItems[i].IsSet = clbReadOnly.GetItemChecked(i);
                i++;
            }
            foreach (ItemSetting item in dset.ReadOnlyItems)
            {
                DataItem di = parentFrom.CurrentSourceBinding.Columns.Find(p => p.ColumnName == item.ColumnName);
                if (di != null)
                {
                    if (item.IsSet)
                    {
                        di.UpdateOption = Platform.Enums.DataItemUpdate.NotAllowed;
                    }
                }
            }
        }
        private void BtnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void btnList_Click(object sender, EventArgs e)
        {
            BindToEntity();
            parentFrom.SaveWindowProfile();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            BindToEntity();
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void rgBehavior_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rgBehavior.SelectedIndex == 3)
            {
                btnQuery.Enabled = true;
            }
            else
                btnQuery.Enabled = false;
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            frmQuery queryForm = new frmQuery(this.parentFrom, this.parentFrom.UQProfile, 1, "", this.parentFrom.GetCurrentDSGrid());
            if (queryForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                WProfile.UserWhere = queryForm.UserWhere;
            }
        }

        private void btnReq_Click(object sender, EventArgs e)
        {
            SetRequire(true);
        }

        private void cButton1_Click(object sender, EventArgs e)
        {
            SetReadOnly(true);
        }

        private void clbReadOnly_ItemChecking(object sender, DevExpress.XtraEditors.Controls.ItemCheckingEventArgs e)
        {
            if (IsLoading)
                return;
            List<ItemSetting> ReadOnlyItems = clbReadOnly.DataSource as List<ItemSetting>;
            if(ReadOnlyItems == null  || ReadOnlyItems.Count < e.Index)
                return;
            ItemSetting item = ReadOnlyItems[e.Index];
            if (item.IsDesignSet)
                e.Cancel = true;
        }

        private void clbRequired_ItemChecking(object sender, DevExpress.XtraEditors.Controls.ItemCheckingEventArgs e)
        {
            if (IsLoading)
                return;
            List<ItemSetting> RequiredItems = clbRequired.DataSource as List<ItemSetting>;
            if (RequiredItems == null || RequiredItems.Count < e.Index)
                return;
            ItemSetting item = RequiredItems[e.Index];
            if (item.IsDesignSet)
                e.Cancel = true;
        }
    }
}
